package com.hzdp.handles.rival.pipelines;

import com.alibaba.fastjson.JSON;
import org.apache.log4j.Logger;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

/**
 * 把抓取的数据根据指定格式存入文件中
 * <p/>
 * Created by leo on 15/12/3.
 */
public class FilePipeline implements Pipeline {

    private final Logger logger = Logger.getLogger(this.getClass());

    private String path = "/Users/lyp/webmagic/";
    private String fileName=new Date().toString();

    public FilePipeline() {
    }

    public FilePipeline(String fileName) {
        this.fileName = fileName;
    }

    public FilePipeline(String path, String fileName) {
        this.path = path;
        this.fileName = fileName;
    }

    @Override
    public void process(ResultItems resultItems, Task task) {
        try {
            if(resultItems.getAll().size()<=0){return;}
            String contents = JSON.toJSONString(resultItems.getAll());
            FileWriter e = new FileWriter(path+fileName+ ".json",true) ;
            e.write(contents);
            e.write("\r\n");
            e.close();
        } catch (IOException var5) {
            this.logger.warn("write file error", var5);
        }
    }
}
